3 * Options for the PHP parser
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\MediaWikiServices
;
25 use Wikimedia\ScopedCallback
;
28 * @brief Set options of the Parser
30 * How to add an option in core:
31 * 1. Add it to one of the arrays in ParserOptions::setDefaults()
32 * 2. If necessary, add an entry to ParserOptions::$inCacheKey
33 * 3. Add a getter and setter in the section for that.
35 * How to add an option in an extension:
36 * 1. Use the 'ParserOptionsRegister' hook to register it.
37 * 2. Where necessary, use $popt->getOption() and $popt->setOption()
45 * Flag indicating that newCanonical() accepts an IContextSource or the string 'canonical', for
46 * back-compat checks from extensions.
49 const HAS_NEWCANONICAL_FROM_CONTEXT
= 1;
52 * Default values for all options that are relevant for caching.
53 * @see self::getDefaults()
56 private static $defaults = null;
62 private static $lazyOptions = [
63 'dateformat' => [ __CLASS__
, 'initDateFormat' ],
67 * Specify options that are included in the cache key
70 private static $inCacheKey = [
72 'numberheadings' => true,
74 'stubthreshold' => true,
80 * Current values for all options that are relevant for caching.
86 * Timestamp used for {{CURRENTDAY}} etc.
88 * @note Caching based on parse time is handled externally
95 * @todo Track this for caching somehow without fragmenting the cache insanely
100 * Function to be called when an option is accessed.
102 * @note Used for collecting used options, does not affect caching
104 private $onAccessCallback = null;
107 * If the page being parsed is a redirect, this should hold the redirect
110 * @todo Track this for caching somehow
112 private $redirectTarget = null;
115 * Appended to the options hash
117 private $mExtraKey = '';
120 * @name Option accessors
125 * Fetch an option, generically
127 * @param string $name Option name
130 public function getOption( $name ) {
131 if ( !array_key_exists( $name, $this->options
) ) {
132 throw new InvalidArgumentException( "Unknown parser option $name" );
135 if ( isset( self
::$lazyOptions[$name] ) && $this->options
[$name] === null ) {
136 $this->options
[$name] = call_user_func( self
::$lazyOptions[$name], $this, $name );
138 if ( !empty( self
::$inCacheKey[$name] ) ) {
139 $this->optionUsed( $name );
141 return $this->options
[$name];
145 * Set an option, generically
147 * @param string $name Option name
148 * @param mixed $value New value. Passing null will set null, unlike many
149 * of the existing accessors which ignore null for historical reasons.
150 * @return mixed Old value
152 public function setOption( $name, $value ) {
153 if ( !array_key_exists( $name, $this->options
) ) {
154 throw new InvalidArgumentException( "Unknown parser option $name" );
156 $old = $this->options
[$name];
157 $this->options
[$name] = $value;
162 * Legacy implementation
163 * @since 1.30 For implementing legacy setters only. Don't use this in new code.
164 * @deprecated since 1.30
165 * @param string $name Option name
166 * @param mixed $value New value. Passing null does not set the value.
167 * @return mixed Old value
169 protected function setOptionLegacy( $name, $value ) {
170 if ( !array_key_exists( $name, $this->options
) ) {
171 throw new InvalidArgumentException( "Unknown parser option $name" );
173 return wfSetVar( $this->options
[$name], $value );
177 * Whether to extract interlanguage links
179 * When true, interlanguage links will be returned by
180 * ParserOutput::getLanguageLinks() instead of generating link HTML.
184 public function getInterwikiMagic() {
185 return $this->getOption( 'interwikiMagic' );
189 * Specify whether to extract interlanguage links
190 * @param bool|null $x New value (null is no change)
191 * @return bool Old value
193 public function setInterwikiMagic( $x ) {
194 return $this->setOptionLegacy( 'interwikiMagic', $x );
198 * Allow all external images inline?
201 public function getAllowExternalImages() {
202 return $this->getOption( 'allowExternalImages' );
206 * Allow all external images inline?
207 * @param bool|null $x New value (null is no change)
208 * @return bool Old value
210 public function setAllowExternalImages( $x ) {
211 return $this->setOptionLegacy( 'allowExternalImages', $x );
215 * External images to allow
217 * When self::getAllowExternalImages() is false
219 * @return string|string[] URLs to allow
221 public function getAllowExternalImagesFrom() {
222 return $this->getOption( 'allowExternalImagesFrom' );
226 * External images to allow
228 * When self::getAllowExternalImages() is false
230 * @param string|string[]|null $x New value (null is no change)
231 * @return string|string[] Old value
233 public function setAllowExternalImagesFrom( $x ) {
234 return $this->setOptionLegacy( 'allowExternalImagesFrom', $x );
238 * Use the on-wiki external image whitelist?
241 public function getEnableImageWhitelist() {
242 return $this->getOption( 'enableImageWhitelist' );
246 * Use the on-wiki external image whitelist?
247 * @param bool|null $x New value (null is no change)
248 * @return bool Old value
250 public function setEnableImageWhitelist( $x ) {
251 return $this->setOptionLegacy( 'enableImageWhitelist', $x );
255 * Automatically number headings?
258 public function getNumberHeadings() {
259 return $this->getOption( 'numberheadings' );
263 * Automatically number headings?
264 * @param bool|null $x New value (null is no change)
265 * @return bool Old value
267 public function setNumberHeadings( $x ) {
268 return $this->setOptionLegacy( 'numberheadings', $x );
272 * Allow inclusion of special pages?
275 public function getAllowSpecialInclusion() {
276 return $this->getOption( 'allowSpecialInclusion' );
280 * Allow inclusion of special pages?
281 * @param bool|null $x New value (null is no change)
282 * @return bool Old value
284 public function setAllowSpecialInclusion( $x ) {
285 return $this->setOptionLegacy( 'allowSpecialInclusion', $x );
289 * Use tidy to cleanup output HTML?
292 public function getTidy() {
293 return $this->getOption( 'tidy' );
297 * Use tidy to cleanup output HTML?
298 * @param bool|null $x New value (null is no change)
299 * @return bool Old value
301 public function setTidy( $x ) {
302 return $this->setOptionLegacy( 'tidy', $x );
306 * Parsing an interface message?
309 public function getInterfaceMessage() {
310 return $this->getOption( 'interfaceMessage' );
314 * Parsing an interface message?
315 * @param bool|null $x New value (null is no change)
316 * @return bool Old value
318 public function setInterfaceMessage( $x ) {
319 return $this->setOptionLegacy( 'interfaceMessage', $x );
323 * Target language for the parse
324 * @return Language|null
326 public function getTargetLanguage() {
327 return $this->getOption( 'targetLanguage' );
331 * Target language for the parse
332 * @param Language|null $x New value
333 * @return Language|null Old value
335 public function setTargetLanguage( $x ) {
336 return $this->setOption( 'targetLanguage', $x );
340 * Maximum size of template expansions, in bytes
343 public function getMaxIncludeSize() {
344 return $this->getOption( 'maxIncludeSize' );
348 * Maximum size of template expansions, in bytes
349 * @param int|null $x New value (null is no change)
350 * @return int Old value
352 public function setMaxIncludeSize( $x ) {
353 return $this->setOptionLegacy( 'maxIncludeSize', $x );
357 * Maximum number of nodes touched by PPFrame::expand()
360 public function getMaxPPNodeCount() {
361 return $this->getOption( 'maxPPNodeCount' );
365 * Maximum number of nodes touched by PPFrame::expand()
366 * @param int|null $x New value (null is no change)
367 * @return int Old value
369 public function setMaxPPNodeCount( $x ) {
370 return $this->setOptionLegacy( 'maxPPNodeCount', $x );
374 * Maximum number of nodes generated by Preprocessor::preprocessToObj()
377 public function getMaxGeneratedPPNodeCount() {
378 return $this->getOption( 'maxGeneratedPPNodeCount' );
382 * Maximum number of nodes generated by Preprocessor::preprocessToObj()
383 * @param int|null $x New value (null is no change)
386 public function setMaxGeneratedPPNodeCount( $x ) {
387 return $this->setOptionLegacy( 'maxGeneratedPPNodeCount', $x );
391 * Maximum recursion depth in PPFrame::expand()
394 public function getMaxPPExpandDepth() {
395 return $this->getOption( 'maxPPExpandDepth' );
399 * Maximum recursion depth for templates within templates
402 public function getMaxTemplateDepth() {
403 return $this->getOption( 'maxTemplateDepth' );
407 * Maximum recursion depth for templates within templates
408 * @param int|null $x New value (null is no change)
409 * @return int Old value
411 public function setMaxTemplateDepth( $x ) {
412 return $this->setOptionLegacy( 'maxTemplateDepth', $x );
416 * Maximum number of calls per parse to expensive parser functions
420 public function getExpensiveParserFunctionLimit() {
421 return $this->getOption( 'expensiveParserFunctionLimit' );
425 * Maximum number of calls per parse to expensive parser functions
427 * @param int|null $x New value (null is no change)
428 * @return int Old value
430 public function setExpensiveParserFunctionLimit( $x ) {
431 return $this->setOptionLegacy( 'expensiveParserFunctionLimit', $x );
435 * Remove HTML comments
436 * @warning Only applies to preprocess operations
439 public function getRemoveComments() {
440 return $this->getOption( 'removeComments' );
444 * Remove HTML comments
445 * @warning Only applies to preprocess operations
446 * @param bool|null $x New value (null is no change)
447 * @return bool Old value
449 public function setRemoveComments( $x ) {
450 return $this->setOptionLegacy( 'removeComments', $x );
454 * Enable limit report in an HTML comment on output
457 public function getEnableLimitReport() {
458 return $this->getOption( 'enableLimitReport' );
462 * Enable limit report in an HTML comment on output
463 * @param bool|null $x New value (null is no change)
464 * @return bool Old value
466 public function enableLimitReport( $x = true ) {
467 return $this->setOptionLegacy( 'enableLimitReport', $x );
471 * Clean up signature texts?
472 * @see Parser::cleanSig
475 public function getCleanSignatures() {
476 return $this->getOption( 'cleanSignatures' );
480 * Clean up signature texts?
481 * @see Parser::cleanSig
482 * @param bool|null $x New value (null is no change)
483 * @return bool Old value
485 public function setCleanSignatures( $x ) {
486 return $this->setOptionLegacy( 'cleanSignatures', $x );
490 * Target attribute for external links
493 public function getExternalLinkTarget() {
494 return $this->getOption( 'externalLinkTarget' );
498 * Target attribute for external links
499 * @param string|null $x New value (null is no change)
500 * @return string Old value
502 public function setExternalLinkTarget( $x ) {
503 return $this->setOptionLegacy( 'externalLinkTarget', $x );
507 * Whether content conversion should be disabled
510 public function getDisableContentConversion() {
511 return $this->getOption( 'disableContentConversion' );
515 * Whether content conversion should be disabled
516 * @param bool|null $x New value (null is no change)
517 * @return bool Old value
519 public function disableContentConversion( $x = true ) {
520 return $this->setOptionLegacy( 'disableContentConversion', $x );
524 * Whether title conversion should be disabled
527 public function getDisableTitleConversion() {
528 return $this->getOption( 'disableTitleConversion' );
532 * Whether title conversion should be disabled
533 * @param bool|null $x New value (null is no change)
534 * @return bool Old value
536 public function disableTitleConversion( $x = true ) {
537 return $this->setOptionLegacy( 'disableTitleConversion', $x );
541 * Thumb size preferred by the user.
544 public function getThumbSize() {
545 return $this->getOption( 'thumbsize' );
549 * Thumb size preferred by the user.
550 * @param int|null $x New value (null is no change)
551 * @return int Old value
553 public function setThumbSize( $x ) {
554 return $this->setOptionLegacy( 'thumbsize', $x );
558 * Thumb size preferred by the user.
561 public function getStubThreshold() {
562 return $this->getOption( 'stubthreshold' );
566 * Thumb size preferred by the user.
567 * @param int|null $x New value (null is no change)
568 * @return int Old value
570 public function setStubThreshold( $x ) {
571 return $this->setOptionLegacy( 'stubthreshold', $x );
575 * Parsing the page for a "preview" operation?
578 public function getIsPreview() {
579 return $this->getOption( 'isPreview' );
583 * Parsing the page for a "preview" operation?
584 * @param bool|null $x New value (null is no change)
585 * @return bool Old value
587 public function setIsPreview( $x ) {
588 return $this->setOptionLegacy( 'isPreview', $x );
592 * Parsing the page for a "preview" operation on a single section?
595 public function getIsSectionPreview() {
596 return $this->getOption( 'isSectionPreview' );
600 * Parsing the page for a "preview" operation on a single section?
601 * @param bool|null $x New value (null is no change)
602 * @return bool Old value
604 public function setIsSectionPreview( $x ) {
605 return $this->setOptionLegacy( 'isSectionPreview', $x );
609 * Parsing the printable version of the page?
612 public function getIsPrintable() {
613 return $this->getOption( 'printable' );
617 * Parsing the printable version of the page?
618 * @param bool|null $x New value (null is no change)
619 * @return bool Old value
621 public function setIsPrintable( $x ) {
622 return $this->setOptionLegacy( 'printable', $x );
626 * Transform wiki markup when saving the page?
629 public function getPreSaveTransform() {
630 return $this->getOption( 'preSaveTransform' );
634 * Transform wiki markup when saving the page?
635 * @param bool|null $x New value (null is no change)
636 * @return bool Old value
638 public function setPreSaveTransform( $x ) {
639 return $this->setOptionLegacy( 'preSaveTransform', $x );
646 public function getDateFormat() {
647 return $this->getOption( 'dateformat' );
651 * Lazy initializer for dateFormat
653 private static function initDateFormat( $popt ) {
654 return $popt->mUser
->getDatePreference();
659 * @param string|null $x New value (null is no change)
660 * @return string Old value
662 public function setDateFormat( $x ) {
663 return $this->setOptionLegacy( 'dateformat', $x );
667 * Get the user language used by the parser for this page and split the parser cache.
669 * @warning: Calling this causes the parser cache to be fragmented by user language!
670 * To avoid cache fragmentation, output should not depend on the user language.
671 * Use Parser::getFunctionLang() or Parser::getTargetLanguage() instead!
673 * @note This function will trigger a cache fragmentation by recording the
674 * 'userlang' option, see optionUsed(). This is done to avoid cache pollution
675 * when the page is rendered based on the language of the user.
677 * @note When saving, this will return the default language instead of the user's.
678 * {{int: }} uses this which used to produce inconsistent link tables (T16404).
683 public function getUserLangObj() {
684 return $this->getOption( 'userlang' );
688 * Same as getUserLangObj() but returns a string instead.
690 * @warning: Calling this causes the parser cache to be fragmented by user language!
691 * To avoid cache fragmentation, output should not depend on the user language.
692 * Use Parser::getFunctionLang() or Parser::getTargetLanguage() instead!
694 * @see getUserLangObj()
696 * @return string Language code
699 public function getUserLang() {
700 return $this->getUserLangObj()->getCode();
704 * Set the user language used by the parser for this page and split the parser cache.
705 * @param string|Language $x New value
706 * @return Language Old value
708 public function setUserLang( $x ) {
709 if ( is_string( $x ) ) {
710 $x = Language
::factory( $x );
713 return $this->setOptionLegacy( 'userlang', $x );
717 * Are magic ISBN links enabled?
721 public function getMagicISBNLinks() {
722 return $this->getOption( 'magicISBNLinks' );
726 * Are magic PMID links enabled?
730 public function getMagicPMIDLinks() {
731 return $this->getOption( 'magicPMIDLinks' );
734 * Are magic RFC links enabled?
738 public function getMagicRFCLinks() {
739 return $this->getOption( 'magicRFCLinks' );
743 * If the wiki is configured to allow raw html ($wgRawHtml = true)
744 * is it allowed in the specific case of parsing this page.
746 * This is meant to disable unsafe parser tags in cases where
747 * a malicious user may control the input to the parser.
749 * @note This is expected to be true for normal pages even if the
750 * wiki has $wgRawHtml disabled in general. The setting only
751 * signifies that raw html would be unsafe in the current context
752 * provided that raw html is allowed at all.
756 public function getAllowUnsafeRawHtml() {
757 return $this->getOption( 'allowUnsafeRawHtml' );
761 * If the wiki is configured to allow raw html ($wgRawHtml = true)
762 * is it allowed in the specific case of parsing this page.
763 * @see self::getAllowUnsafeRawHtml()
765 * @param bool|null $x Value to set or null to get current value
766 * @return bool Current value for allowUnsafeRawHtml
768 public function setAllowUnsafeRawHtml( $x ) {
769 return $this->setOptionLegacy( 'allowUnsafeRawHtml', $x );
773 * Class to use to wrap output from Parser::parse()
775 * @return string|bool
777 public function getWrapOutputClass() {
778 return $this->getOption( 'wrapclass' );
782 * CSS class to use to wrap output from Parser::parse()
784 * @param string $className Class name to use for wrapping.
785 * Passing false to indicate "no wrapping" was deprecated in MediaWiki 1.31.
786 * @return string|bool Current value
788 public function setWrapOutputClass( $className ) {
789 if ( $className === true ) { // DWIM, they probably want the default class name
790 $className = 'mw-parser-output';
792 if ( $className === false ) {
793 wfDeprecated( __METHOD__
. '( false )', '1.31' );
795 return $this->setOption( 'wrapclass', $className );
799 * Callback for current revision fetching; first argument to call_user_func().
803 public function getCurrentRevisionCallback() {
804 return $this->getOption( 'currentRevisionCallback' );
808 * Callback for current revision fetching; first argument to call_user_func().
810 * @param callable|null $x New value (null is no change)
811 * @return callable Old value
813 public function setCurrentRevisionCallback( $x ) {
814 return $this->setOptionLegacy( 'currentRevisionCallback', $x );
818 * Callback for template fetching; first argument to call_user_func().
821 public function getTemplateCallback() {
822 return $this->getOption( 'templateCallback' );
826 * Callback for template fetching; first argument to call_user_func().
827 * @param callable|null $x New value (null is no change)
828 * @return callable Old value
830 public function setTemplateCallback( $x ) {
831 return $this->setOptionLegacy( 'templateCallback', $x );
835 * Callback to generate a guess for {{REVISIONID}}
837 * @return callable|null
839 public function getSpeculativeRevIdCallback() {
840 return $this->getOption( 'speculativeRevIdCallback' );
844 * Callback to generate a guess for {{REVISIONID}}
846 * @param callable|null $x New value (null is no change)
847 * @return callable|null Old value
849 public function setSpeculativeRevIdCallback( $x ) {
850 return $this->setOptionLegacy( 'speculativeRevIdCallback', $x );
856 * Timestamp used for {{CURRENTDAY}} etc.
859 public function getTimestamp() {
860 if ( !isset( $this->mTimestamp
) ) {
861 $this->mTimestamp
= wfTimestampNow();
863 return $this->mTimestamp
;
867 * Timestamp used for {{CURRENTDAY}} etc.
868 * @param string|null $x New value (null is no change)
869 * @return string Old value
871 public function setTimestamp( $x ) {
872 return wfSetVar( $this->mTimestamp
, $x );
876 * Create "edit section" links?
877 * @deprecated since 1.31, use ParserOutput::getText() options instead.
880 public function getEditSection() {
881 wfDeprecated( __METHOD__
, '1.31' );
886 * Create "edit section" links?
887 * @deprecated since 1.31, use ParserOutput::getText() options instead.
888 * @param bool|null $x New value (null is no change)
889 * @return bool Old value
891 public function setEditSection( $x ) {
892 wfDeprecated( __METHOD__
, '1.31' );
897 * Set the redirect target.
899 * Note that setting or changing this does not *make* the page a redirect
900 * or change its target, it merely records the information for reference
904 * @param Title|null $title
906 function setRedirectTarget( $title ) {
907 $this->redirectTarget
= $title;
911 * Get the previously-set redirect target.
916 function getRedirectTarget() {
917 return $this->redirectTarget
;
921 * Extra key that should be present in the parser cache key.
922 * @warning Consider registering your additional options with the
923 * ParserOptionsRegister hook instead of using this method.
926 public function addExtraKey( $key ) {
927 $this->mExtraKey
.= '!' . $key;
934 public function getUser() {
939 * @warning For interaction with the parser cache, use
940 * WikiPage::makeParserOptions() or ParserOptions::newCanonical() instead.
941 * @param User|null $user
942 * @param Language|null $lang
944 public function __construct( $user = null, $lang = null ) {
945 if ( $user === null ) {
947 if ( $wgUser === null ) {
953 if ( $lang === null ) {
955 if ( !StubObject
::isRealObject( $wgLang ) ) {
960 $this->initialiseFromUser( $user, $lang );
964 * Get a ParserOptions object for an anonymous user
965 * @warning For interaction with the parser cache, use
966 * WikiPage::makeParserOptions() or ParserOptions::newCanonical() instead.
968 * @return ParserOptions
970 public static function newFromAnon() {
972 return new ParserOptions( new User
, $wgContLang );
976 * Get a ParserOptions object from a given user.
977 * Language will be taken from $wgLang.
979 * @warning For interaction with the parser cache, use
980 * WikiPage::makeParserOptions() or ParserOptions::newCanonical() instead.
982 * @return ParserOptions
984 public static function newFromUser( $user ) {
985 return new ParserOptions( $user );
989 * Get a ParserOptions object from a given user and language
991 * @warning For interaction with the parser cache, use
992 * WikiPage::makeParserOptions() or ParserOptions::newCanonical() instead.
994 * @param Language $lang
995 * @return ParserOptions
997 public static function newFromUserAndLang( User
$user, Language
$lang ) {
998 return new ParserOptions( $user, $lang );
1002 * Get a ParserOptions object from a IContextSource object
1004 * @warning For interaction with the parser cache, use
1005 * WikiPage::makeParserOptions() or ParserOptions::newCanonical() instead.
1006 * @param IContextSource $context
1007 * @return ParserOptions
1009 public static function newFromContext( IContextSource
$context ) {
1010 return new ParserOptions( $context->getUser(), $context->getLanguage() );
1014 * Creates a "canonical" ParserOptions object
1016 * For historical reasons, certain options have default values that are
1017 * different from the canonical values used for caching.
1020 * @since 1.32 Added string and IContextSource as options for the first parameter
1021 * @param IContextSource|string|User|null $context
1022 * - If an IContextSource, the options are initialized based on the source's User and Language.
1023 * - If the string 'canonical', the options are initialized with an anonymous user and
1025 * - If a User or null, the options are initialized for that User (or $wgUser if null).
1026 * 'userlang' is taken from the $userLang parameter, defaulting to $wgLang if that is null.
1027 * @param Language|StubObject|null $userLang (see above)
1028 * @return ParserOptions
1030 public static function newCanonical( $context = null, $userLang = null ) {
1031 if ( $context instanceof IContextSource
) {
1032 $ret = self
::newFromContext( $context );
1033 } elseif ( $context === 'canonical' ) {
1034 $ret = self
::newFromAnon();
1035 } elseif ( $context instanceof User ||
$context === null ) {
1036 $ret = new self( $context, $userLang );
1038 throw new InvalidArgumentException(
1039 '$context must be an IContextSource, the string "canonical", a User, or null'
1043 foreach ( self
::getCanonicalOverrides() as $k => $v ) {
1044 $ret->setOption( $k, $v );
1050 * Get default option values
1051 * @warning If you change the default for an existing option (unless it's
1052 * being overridden by self::getCanonicalOverrides()), all existing parser
1053 * cache entries will be invalid. To avoid bugs, you'll need to handle
1054 * that somehow (e.g. with the RejectParserCacheValue hook) because
1055 * MediaWiki won't do it for you.
1058 private static function getDefaults() {
1059 global $wgInterwikiMagic, $wgAllowExternalImages,
1060 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
1061 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
1062 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
1063 $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion,
1064 $wgEnableMagicLinks, $wgContLang;
1066 if ( self
::$defaults === null ) {
1067 // *UPDATE* ParserOptions::matches() if any of this changes as needed
1069 'dateformat' => null,
1071 'interfaceMessage' => false,
1072 'targetLanguage' => null,
1073 'removeComments' => true,
1074 'enableLimitReport' => false,
1075 'preSaveTransform' => true,
1076 'isPreview' => false,
1077 'isSectionPreview' => false,
1078 'printable' => false,
1079 'allowUnsafeRawHtml' => true,
1080 'wrapclass' => 'mw-parser-output',
1081 'currentRevisionCallback' => [ Parser
::class, 'statelessFetchRevision' ],
1082 'templateCallback' => [ Parser
::class, 'statelessFetchTemplate' ],
1083 'speculativeRevIdCallback' => null,
1086 Hooks
::run( 'ParserOptionsRegister', [
1089 &self
::$lazyOptions,
1092 ksort( self
::$inCacheKey );
1095 // Unit tests depend on being able to modify the globals at will
1096 return self
::$defaults +
[
1097 'interwikiMagic' => $wgInterwikiMagic,
1098 'allowExternalImages' => $wgAllowExternalImages,
1099 'allowExternalImagesFrom' => $wgAllowExternalImagesFrom,
1100 'enableImageWhitelist' => $wgEnableImageWhitelist,
1101 'allowSpecialInclusion' => $wgAllowSpecialInclusion,
1102 'maxIncludeSize' => $wgMaxArticleSize * 1024,
1103 'maxPPNodeCount' => $wgMaxPPNodeCount,
1104 'maxGeneratedPPNodeCount' => $wgMaxGeneratedPPNodeCount,
1105 'maxPPExpandDepth' => $wgMaxPPExpandDepth,
1106 'maxTemplateDepth' => $wgMaxTemplateDepth,
1107 'expensiveParserFunctionLimit' => $wgExpensiveParserFunctionLimit,
1108 'externalLinkTarget' => $wgExternalLinkTarget,
1109 'cleanSignatures' => $wgCleanSignatures,
1110 'disableContentConversion' => $wgDisableLangConversion,
1111 'disableTitleConversion' => $wgDisableLangConversion ||
$wgDisableTitleConversion,
1112 'magicISBNLinks' => $wgEnableMagicLinks['ISBN'],
1113 'magicPMIDLinks' => $wgEnableMagicLinks['PMID'],
1114 'magicRFCLinks' => $wgEnableMagicLinks['RFC'],
1115 'numberheadings' => User
::getDefaultOption( 'numberheadings' ),
1116 'thumbsize' => User
::getDefaultOption( 'thumbsize' ),
1117 'stubthreshold' => 0,
1118 'userlang' => $wgContLang,
1123 * Get "canonical" non-default option values
1124 * @see self::newCanonical
1125 * @warning If you change the override for an existing option, all existing
1126 * parser cache entries will be invalid. To avoid bugs, you'll need to
1127 * handle that somehow (e.g. with the RejectParserCacheValue hook) because
1128 * MediaWiki won't do it for you.
1131 private static function getCanonicalOverrides() {
1132 global $wgEnableParserLimitReporting;
1136 'enableLimitReport' => $wgEnableParserLimitReporting,
1144 * @param Language $lang
1146 private function initialiseFromUser( $user, $lang ) {
1147 $this->options
= self
::getDefaults();
1149 $this->mUser
= $user;
1150 $this->options
['numberheadings'] = $user->getOption( 'numberheadings' );
1151 $this->options
['thumbsize'] = $user->getOption( 'thumbsize' );
1152 $this->options
['stubthreshold'] = $user->getStubThreshold();
1153 $this->options
['userlang'] = $lang;
1157 * Check if these options match that of another options set
1159 * This ignores report limit settings that only affect HTML comments
1161 * @param ParserOptions $other
1165 public function matches( ParserOptions
$other ) {
1166 // Populate lazy options
1167 foreach ( self
::$lazyOptions as $name => $callback ) {
1168 if ( $this->options
[$name] === null ) {
1169 $this->options
[$name] = call_user_func( $callback, $this, $name );
1171 if ( $other->options
[$name] === null ) {
1172 $other->options
[$name] = call_user_func( $callback, $other, $name );
1176 // Compare most options
1177 $options = array_keys( $this->options
);
1178 $options = array_diff( $options, [
1179 'enableLimitReport', // only affects HTML comments
1181 foreach ( $options as $option ) {
1182 $o1 = $this->optionToString( $this->options
[$option] );
1183 $o2 = $this->optionToString( $other->options
[$option] );
1184 if ( $o1 !== $o2 ) {
1189 // Compare most other fields
1190 $fields = array_keys( get_class_vars( __CLASS__
) );
1191 $fields = array_diff( $fields, [
1192 'defaults', // static
1193 'lazyOptions', // static
1194 'inCacheKey', // static
1195 'options', // Already checked above
1196 'onAccessCallback', // only used for ParserOutput option tracking
1198 foreach ( $fields as $field ) {
1199 if ( !is_object( $this->$field ) && $this->$field !== $other->$field ) {
1208 * Registers a callback for tracking which ParserOptions which are used.
1209 * This is a private API with the parser.
1210 * @param callable $callback
1212 public function registerWatcher( $callback ) {
1213 $this->onAccessCallback
= $callback;
1217 * Called when an option is accessed.
1218 * Calls the watcher that was set using registerWatcher().
1219 * Typically, the watcher callback is ParserOutput::registerOption().
1220 * The information registered that way will be used by ParserCache::save().
1222 * @param string $optionName Name of the option
1224 public function optionUsed( $optionName ) {
1225 if ( $this->onAccessCallback
) {
1226 call_user_func( $this->onAccessCallback
, $optionName );
1231 * Returns the full array of options that would have been used by
1233 * Used to get the old parser cache entries when available.
1234 * @deprecated since 1.30. You probably want self::allCacheVaryingOptions() instead.
1237 public static function legacyOptions() {
1238 wfDeprecated( __METHOD__
, '1.30' );
1250 * Return all option keys that vary the options hash
1254 public static function allCacheVaryingOptions() {
1255 // Trigger a call to the 'ParserOptionsRegister' hook if it hasn't
1256 // already been called.
1257 if ( self
::$defaults === null ) {
1258 self
::getDefaults();
1260 return array_keys( array_filter( self
::$inCacheKey ) );
1264 * Convert an option to a string value
1265 * @param mixed $value
1268 private function optionToString( $value ) {
1269 if ( $value === true ) {
1271 } elseif ( $value === false ) {
1273 } elseif ( $value === null ) {
1275 } elseif ( $value instanceof Language
) {
1276 return $value->getCode();
1277 } elseif ( is_array( $value ) ) {
1278 return '[' . implode( ',', array_map( [ $this, 'optionToString' ], $value ) ) . ']';
1280 return (string)$value;
1285 * Generate a hash string with the values set on these ParserOptions
1286 * for the keys given in the array.
1287 * This will be used as part of the hash key for the parser cache,
1288 * so users sharing the options with vary for the same page share
1289 * the same cached data safely.
1292 * @param string[] $forOptions
1293 * @param Title|null $title Used to get the content language of the page (since r97636)
1294 * @return string Page rendering hash
1296 public function optionsHash( $forOptions, $title = null ) {
1297 global $wgRenderHashAppend;
1299 $inCacheKey = self
::allCacheVaryingOptions();
1301 // Resolve any lazy options
1302 foreach ( array_intersect( $forOptions, $inCacheKey, array_keys( self
::$lazyOptions ) ) as $k ) {
1303 if ( $this->options
[$k] === null ) {
1304 $this->options
[$k] = call_user_func( self
::$lazyOptions[$k], $this, $k );
1308 $options = $this->options
;
1309 $defaults = self
::getCanonicalOverrides() + self
::getDefaults();
1311 // We only include used options with non-canonical values in the key
1312 // so adding a new option doesn't invalidate the entire parser cache.
1313 // The drawback to this is that changing the default value of an option
1314 // requires manual invalidation of existing cache entries, as mentioned
1315 // in the docs on the relevant methods and hooks.
1317 foreach ( array_intersect( $inCacheKey, $forOptions ) as $option ) {
1318 $v = $this->optionToString( $options[$option] );
1319 $d = $this->optionToString( $defaults[$option] );
1321 $values[] = "$option=$v";
1325 $confstr = $values ?
implode( '!', $values ) : 'canonical';
1327 // add in language specific options, if any
1328 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
1329 if ( !is_null( $title ) ) {
1330 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
1333 $confstr .= $wgContLang->getExtraHashOptions();
1336 $confstr .= $wgRenderHashAppend;
1338 if ( $this->mExtraKey
!= '' ) {
1339 $confstr .= $this->mExtraKey
;
1342 // Give a chance for extensions to modify the hash, if they have
1343 // extra options or other effects on the parser cache.
1344 Hooks
::run( 'PageRenderingHash', [ &$confstr, $this->getUser(), &$forOptions ] );
1346 // Make it a valid memcached key fragment
1347 $confstr = str_replace( ' ', '_', $confstr );
1353 * Test whether these options are safe to cache
1357 public function isSafeToCache() {
1358 $defaults = self
::getCanonicalOverrides() + self
::getDefaults();
1359 foreach ( $this->options
as $option => $value ) {
1360 if ( empty( self
::$inCacheKey[$option] ) ) {
1361 $v = $this->optionToString( $value );
1362 $d = $this->optionToString( $defaults[$option] );
1372 * Sets a hook to force that a page exists, and sets a current revision callback to return
1373 * a revision with custom content when the current revision of the page is requested.
1376 * @param Title $title
1377 * @param Content $content
1378 * @param User $user The user that the fake revision is attributed to
1379 * @return ScopedCallback to unset the hook
1381 public function setupFakeRevision( $title, $content, $user ) {
1382 $oldCallback = $this->setCurrentRevisionCallback(
1384 $titleToCheck, $parser = false ) use ( $title, $content, $user, &$oldCallback
1386 if ( $titleToCheck->equals( $title ) ) {
1387 return new Revision( [
1388 'page' => $title->getArticleID(),
1389 'user_text' => $user->getName(),
1390 'user' => $user->getId(),
1391 'parent_id' => $title->getLatestRevID(),
1393 'content' => $content
1396 return call_user_func( $oldCallback, $titleToCheck, $parser );
1402 $wgHooks['TitleExists'][] =
1403 function ( $titleToCheck, &$exists ) use ( $title ) {
1404 if ( $titleToCheck->equals( $title ) ) {
1408 end( $wgHooks['TitleExists'] );
1409 $key = key( $wgHooks['TitleExists'] );
1410 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
1411 $linkCache->clearBadLink( $title->getPrefixedDBkey() );
1412 return new ScopedCallback( function () use ( $title, $key, $linkCache ) {
1414 unset( $wgHooks['TitleExists'][$key] );
1415 $linkCache->clearLink( $title );
1421 * For really cool vim folding this needs to be at the end:
1422 * vim: foldmarker=@{,@} foldmethod=marker